package org.microservice.system.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.microservice.common.util.Query;
import org.microservice.common.util.R;
import org.microservice.system.service.SysGeneratorService;
import org.microservice.system.utils.GenerateCodeUtil;
import org.microservice.system.utils.dto.ColumnEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/7/25
 * Time: 15:48
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@RestController
@RequestMapping("/generator")
public class SysGeneratorController{

    @Autowired
    private SysGeneratorService sysGeneratorService;


    @Autowired
    private GenerateCodeUtil generateCodeUtil;

    /**
     * 查询数据库的表
     * @param params
     * @return
     */
    @PostMapping("/getTableListPage")
    public R getTableListPage(@RequestBody Map<String,Object> params){
        Query query = new Query(params);
        List<Map<String,Object>> tableEntities = sysGeneratorService.getTableListPage(query);
        int total = sysGeneratorService.getTableListPageTotal(query);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",tableEntities);
        jsonObject.put("total",total);
        return R.ok().put("data",jsonObject);
    }

    /**
     * 由表名查询字段
     * @param tableName
     * @return
     */
    @GetMapping("/getTableColumnListPage")
    public R getTableColumnListPage(@RequestParam(value = "tableName") String tableName){
        List<ColumnEntity> tableColumns = sysGeneratorService.getTableColumns(tableName);

        return R.ok().put("data",tableColumns);
    }

    /**
     * 生产代码
     * @param params
     * @param request
     * @param response
     * @throws Exception
     */
    @PostMapping("/generateCode")
    public void generateCode(@RequestBody Map<String,Object> params, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String packageName = (String) params.get("packageName");
        String moduleName = (String) params.get("moduleName");
        List<String> tableNames = (List<String>) params.get("tableName");
        String[] strings = new String[tableNames.size()];


        String downloadName = "压缩文件.zip";
         //将文件进行打包下载

        OutputStream out = response.getOutputStream();
        byte[] data = generateCodeUtil.generate(packageName, moduleName, tableNames.toArray(strings));
        response.reset();
        response.setHeader("Content-Disposition","attachment;fileName="+downloadName);
        response.addHeader("Content-Length", ""+data.length);
        response.setContentType("application/octet-stream;charset=UTF-8");
        IOUtils.write(data, out);
        out.flush();
        out.close();

    }



}
